package com.erp.module.zhiLiang

import android.content.Context
import android.util.Log
import com.erp.bean.ZhiLiangBean
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.PhotoBean
import com.erp.pull.BasePullP
import com.erp.util.JsonUtil
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.util.*

/**
 * Created by Administrator on 2019/3/20/020.
 * 作者：刘洋
 */
class IZhiLiangP(var V : ZhiLiangContract.ZhiLiangView,var context: Context,var list: MutableList<ZhiLiangBean>) : BasePullP(),ZhiLiangContract.ZhiLiangP{


    var md = ZhiLiangModel()
    var sousuoBean = ZhiLiangSouSuoBean()
    init {
        PullV = V
    }
    override fun getData(page: Int, pagesize: Int, onUserDataInterface: OnUserDataInterface) {
        md.getBanCaiZhiLiang(context,page,pagesize,sousuoBean,onUserDataInterface)
    }

    override fun jsondata(data: String?) {
        if (data==null){
            return
        }
        try {
            Log.d("TAAG", "data=$data")
            val js = JSONArray(data)
            for (i in 0 until js.length()) {
                val bean = ZhiLiangBean()
                val jo = js.get(i) as JSONObject
                bean.id = JsonUtil.getvolue(jo, "id")
                bean.oid = JsonUtil.getvolue(jo, "oid")
                bean.mcode = JsonUtil.getvolue(jo, "mcode")
                bean.work_number = JsonUtil.getvolue(jo, "work_number")
                bean.occurrence_time = JsonUtil.getvolue(jo, "occurrence_time")
                bean.description = JsonUtil.getvolue(jo, "description")
                bean.handle_way = JsonUtil.getvolue(jo, "handle_way")
                bean.ctime = JsonUtil.getvolue(jo, "ctime")
                bean.qi_name = JsonUtil.getvolue(jo, "qi_name")
                bean.qi_id = JsonUtil.getvolue(jo, "qi_id")
                bean.creator_uid = JsonUtil.getvolue(jo, "creator_uid")
                bean.creator_name = JsonUtil.getvolue(jo, "creator_name")
                bean.dnum = JsonUtil.getvolue(jo, "dnum")
                bean.solution = JsonUtil.getvolue(jo, "solution")
                bean.han_qu = JsonUtil.getvolue(jo, "han_qu")
                bean.bu_men = JsonUtil.getvolue(jo, "bu_men")
                bean.che_jian = JsonUtil.getvolue(jo, "che_jian")
                bean.zu_bie = JsonUtil.getvolue(jo, "zu_bie")
                bean.wu_name = JsonUtil.getvolue(jo, "wu_name")
                bean.count = JsonUtil.getvolue(jo, "count")
                bean.ts = JsonUtil.getvolue(jo, "ts")
                bean.mb_batch_name = JsonUtil.getvolue(jo, "mb_batch_name")
                bean.pname = JsonUtil.getvolue(jo, "pname")
                bean.consignee_time = JsonUtil.getvolue(jo, "consignee_time")
                bean.consignee_name = JsonUtil.getvolue(jo, "consignee_name")
                val jsp = JsonUtil.getjsvolue(jo, "files")
                if (jsp != null) {
                    for (j in 0 until jsp.length()) {
                        val jop = jsp.get(j) as JSONObject
                        val photo = PhotoBean()
                        photo.fid = JsonUtil.getvolue(jop, "fid")
                        photo.filename = JsonUtil.getvolue(jop, "filename")
                        photo.uri = JsonUtil.getvolue(jop, "uri")
                        bean.listphoto.add(photo)
                    }
                }
                val bujs= JsonUtil.getjsvolue(jo,"buzhou")
                if(bujs!=null) {
                    for (k in 0 until bujs.length()) {
                        val jop = bujs.get(k) as JSONObject
                        val buzhou = ZhiLiangBean.buzhou()
                        buzhou.gangwei_name = JsonUtil.getvolue(jop, "gangwei_name")
                        buzhou.ctime = JsonUtil.getvolue(jop, "ctime")
                        bean.listbuzhou.add(buzhou)
                    }
                }
                list.add(bean)
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }
    }

    override fun listClean() {
        list.clear()
    }

    override fun guolv(sousuobean: ZhiLiangSouSuoBean) {
        this.sousuoBean=sousuobean
        xiala()
    }

    override fun getWuLiao_DaiMa() {
        var zhiliangid: MutableList<String> = ArrayList()
        var zhiliangname: MutableList<String> = ArrayList()
        md.getWuLiao_LeiBie_DaiMa(context,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                val jo = JSONObject(data)
                val keys = jo.keys()
                while (keys.hasNext()) {
                    val key = keys.next()
                    zhiliangid.add(key)
                    zhiliangname.add(JsonUtil.getvolue(jo, key))
                }
                V.getWuLiao_DaiMaOK(zhiliangid,zhiliangname)
            }

            override fun onNot(throwable: String?) {
                V.getWuLiao_DaiMaNO()
            }

        })
    }

    override fun shouhuo(id: String) {
        md.querenShouhuo(context,id,object :OnUserDataInterface{
            override fun onUserData(data: String?) {
                V.shouhuoOk()
            }

            override fun onNot(throwable: String?) {
                V.shouhuoNo()
            }

        })
    }

    class ZhiLiangSouSuoBean{
        var wenti=""
        var sousuo = ""
        var quhao = ""
        var startTime = ""
        var endTime = ""
        var wuliao = ""
        var time_category =""
    }
}